package library.gui.scene;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.layout.BorderPane;
import library.core.user.User;
import library.gui.SystemApplication;
import library.gui.component.FunctionalButton;
import library.gui.component.HButtonList;
import library.gui.component.InputList;
import library.gui.component.TitleLabel;
import library.gui.dialog.NotiDialog;

public class LoginPane extends BorderPane{

	private SystemApplication handler;
	
	private int width = 300;
	
	private TitleLabel title = new TitleLabel("登录");
	private InputList inputList = new InputList();
	private HButtonList buttonList;
	
	public LoginPane(SystemApplication handler) {		
		
		this.handler = handler;
		
		this.setPrefWidth(width);
		
		inputList.prefWidthProperty().bind(this.widthProperty());
		inputList.addKey("ID").addSecretedKey("密码");
		inputList.setPadding(new Insets(10));
		
		BorderPane.setAlignment(title, Pos.CENTER);
		this.setCenter(inputList);
	
		buttonList = new HButtonList(
				new FunctionalButton("登录", ()->{
					User user = this.handler.getLibrary().login(
							inputList.get("ID"), inputList.get("密码"));
					if(user == null)
						new NotiDialog("登录失败", "请检查用户名和密码是否正确").showAndWait();
					else {
						this.handler.setCurrentUser(user);
						this.handler.toOperateScene();	
					}
				}),
				new FunctionalButton("返回", this.handler::toMainMenuScene),
				new FunctionalButton("注册", this.handler::toRegisterScene),
				new FunctionalButton("忘记密码", ()->{
					new NotiDialog("请联系管理员", "邮箱：XXX-YYY@ZZZ.org").showAndWait();
				}));
		buttonList.prefWidthProperty().bind(this.widthProperty());
		
		this.setBottom(buttonList);
		this.setTop(title);
		
	}
	
}
